<?php

// autor/i etapy:
// 29.11.2013

class Model_admin extends CI_Model {

    public function get_id_udalosti($temp){
        $this->db->where('nazov', $temp);
        $user = $this->db->get('udalost');
        if ($user) {
             $row = $user->row();
             $user_check = $row->id;
             return $user_check;
        }  
        return NULL;
    }
    
    public function get_zamerania(){
        $results = array();
        $this->db->select('zameranie');
        $this->db->from('zameranie');

        $query = $this->db->get();
        foreach ($query->result() as $row){
            $results[] = $row->zameranie;
        }
        return $results;
    }
   
    
    public function get_udalosti(){
        $results = array();
        $this->db->select('nazov');
        $this->db->from('udalost');

        $query = $this->db->get();
        foreach ($query->result() as $row){
            $results[] = $row->nazov;
        }
        return $results;
	}
	
	  public function get_info_udalosti($key){
        $this->db->where('id', $key);
        $user = $this->db->get('udalost');
        if ($user) {
             $row = $user->row();
             $udalost = array(
                  'zameranie' => $row->id_zameranie,
                  'miestnost' => $row->miestnost,
                  'ucitel' => $row->ucitel,
                  'nazov' => $row->nazov,
                  'abstrakt' => $row->abstrakt,
                  'maxLudi' => $row->maxludi,
                  'poznamka' => $row->poznamka,
                  'maxprednasok' => $row->maxprednasok,
                  'id_ud' => $row->id
             );
             return $udalost;
        }  
        return NULL;
    } 
    
    public function addUdalost(){
        $data = array(
            'nazov' => $this->input->post('nazov'),
            'miestnost' => $this->input->post('miestnost'),
            'id_zameranie' => $this->input->post('zameranie'),
            'ucitel' => $this->input->post('ucitel'),
            'abstrakt' => $this->input->post('abstrakt'),
            'maxludi' => $this->input->post('maxLud'),
            'poznamka' => $this->input->post('poznamka'),
            'maxprednasok' => $this->input->post('maxprednasok') 
        );     
        $query = $this->db->insert('udalost',$data);
        if ($query){
            return true;
        } else{
            return false;
        }
    }
    
    public function addTermin($udalost){
        $data = array(
            'id_udalosti' => $udalost,
            'cas_od' => $this->input->post('casOd'),
            'cas_do' => $this->input->post('casDo'),
            'trvanie' => $this->input->post('trvanie'),
            'zaplnenie' => 0,
            'den' => $this->input->post('den'),
            'fixna' => $this->input->post('fixCas') 
        );     
        $query = $this->db->insert('casy_udalosti',$data);
        if ($query){
            return true;
        } else{
            return false;
        }
    }
	
	public function zmenit_Udalost($id){
        $this->db->where('id', $id);
        $data = array(
            'id_zameranie' => $this->input->post('zameranie'),
            'miestnost' => $this->input->post('miestnost'),
            'ucitel' => $this->input->post('ucitel'),
            'nazov' => $this->input->post('nazov'),
            'abstrakt' => $this->input->post('abstrakt'),
            'maxludi' => $this->input->post('maxLud'),
			'poznamka' => $this->input->post('poznamka'),
			'maxprednasok' => $this->input->post('maxprednasok')
        );
        if ($this->db->update('udalost', $data))
            return true;
        else 
			return false;
    }
	
	public function zmena_casu_udalosti($id){
        $this->db->where('id', $id);
        $data = array(
            'den' => $this->input->post('den'),
            'cas_od' => $this->input->post('casOd'),
            'cas_do' => $this->input->post('casDo'),
            'trvanie' => $this->input->post('trvanie'),
            'fixna' => $this->input->post('fixCas')
        );
        if ($this->db->update('casy_udalosti', $data))
            return true;
        else 
			return false;
    }
	
	public function zmazat_Udalost($id){
		$this->db->where('id_udalosti', $id);
       $this->db->select('id');
	   $this->db->from('casy_udalosti');
	   $query = $this->db->get();
	   $rozvrh_result = true;
	   foreach ($query->result() as $row){
            if(!$this->clear_rozvrh($row->id)){
				$rozvrh_result = false;
			}
        }
		if(!$rozvrh_result){
			return false;	
		}
		$this->db->where('id_udalosti', $id);
       if (!$this->db->delete('casy_udalosti')){
          return false;
	   }
	   $this->db->where('id', $id);
	   if (!$this->db->delete('udalost')){
          return false;
	   }
       return true;
    }
	
	
	public function zmazat_cas_udalosti($id){
		$this->db->where('id', $id);
		$this->db->from('casy_udalosti');
	   $query = $this->db->get();
	   $rozvrh_result = true;
	   foreach ($query->result() as $row){
            if(!$this->clear_rozvrh($row->id)){
				$rozvrh_result = false;
			}
        }
		if(!$rozvrh_result){
			return false;	
		}
		 $this->db->where('id', $id);
       if (!$this->db->delete('casy_udalosti')){
          return false;
	   }
       return true;
    }
	
	public function clear_rozvrh($id){
		$this->db->where('id_cas_udalosti', $id);
		$this->db->from('rozvrh');
		$this->db->select('id_kolektiv');
		$query = $this->db->get();
		foreach ($query->result() as $row){
            $this->poslat_mail_o_zmene_userom($row->id_kolektiv);
        }
		$this->db->where('id_cas_udalosti', $id);
		if ($this->db->delete('rozvrh'))
          return true;
       else 
	   		return false;	
	}
	
	public function poslat_mail_o_zmene_userom($id_kolektiv){
		$this->db->select('id_uzivatela');
		$this->db->from('kolektiv');
    $this->db->where('id',$id_kolektiv);
		$query = $this->db->get();
			if($query){
				$row = $query->row();
				$user_id = $row->id_uzivatela;
				$email = $this->get_uzivatel_email($user_id); // TODO sem poslat mail
        
          $this->email->from('noreply@PPTV.com', "noreply");
          $this->email->to($email);
          $this->email->subject("Zmena rozvrhu");

          $message = "<p>Vazeny uzivatel!</p>";
          $message .= "<p>Váš rozvrh sa zmenil, jeho aktualizovanú verziu si môžete pozrieť <a href='".base_url()."rozvrh' >tu! </a> </p>";

          $this->email->message($message);
          $this->email->send();
          
			}
		return false;
	}
	
	public function get_uzivatel_email($id){
		$this->db->where('id', $id);
		$this->db->from('uzivatelia');
		$query = $this->db->get();
		if($query){
				$row = $query->row();
				return $row->email;
			}
	}
  
  public function get_prihlasene_kolektivy($key){
      $results = array();
      $this->db->select('id_kolektiv');
      $this->db->from('rozvrh');
      $this->db->where('id_cas_udalosti', $key);

      $query = $this->db->get();
      foreach ($query->result() as $row){
          $results[] = $row->id_kolektiv;
      }
      return $results;
  }
  
  public function get_ucitel_id($key){
        $this->db->where('id', $key);
        $user = $this->db->get('kolektiv');
        if ($user) {
             $row = $user->row();
             $user_check = $row->id_uzivatela;
             return $user_check;
        }  
        return NULL;
  }
  
  public function get_ucitel_info($key){
        $this->db->where('id', $key);
        $user = $this->db->get('uzivatelia');
        if ($user) {
             $row = $user->row();
             $ucitel = array(
                  'meno' => $row->meno,
                  'priezvisko' => $row->priezvisko,
                  'email' => $row->email,
                  'telefon' => $row->telefon,
                  'skola' => $row->skola
             );
             return $ucitel;
        }  
        return NULL;
  }
  
  public function zmena_emailu($key, $temp){
      $this->db->where('email', $key);
        $data = array(
            'email' => $temp
        );
        if ($this->db->update('uzivatelia', $data))
            return true;
        else 
			return false;
  }
  
    public function zmena_textu($key, $text){
      $this->db->where('text_name', $key);
        $data = array(
            'text' => $text
        );
        if ($this->db->update('text_constants', $data))
            return true;
        else 
			return false;
  }
  
  public function zmaz_terminy(){
      $this->db->empty_table('rozvrh');
	  $this->db->empty_table('casy_udalosti');
  }
  
  public function zmaz_udalosti(){
	$this->db->empty_table('udalost');
  }
  
}
